﻿#include <stdlib.h>
#include <libunits\units.h>
#include <libunits\units_c.h>

#include <cstdio>

// 自定义日志回调函数
void my_logger(const char *level, const char *message) {
  printf("[%s] %s\n", level, message);
}

void convert_to_base(UnitsHandle handle, const char *dimension, double from) {
  UnitsErrorCode result;
  double to = 0.0;
  result = units_convert_to_base(handle, from, dimension, &to);
  char base[256] = {'\0'};
  units_get_base_unit(handle, dimension, base, 256);
  char current[256] = {'\0'};
  units_get_current_unit(handle, dimension, current, 256);
  printf("%s base unit: %s, current unit: %s\n", dimension, base, current);
  printf("%.4f %s = %.4f %s (base unit) (%s)\n", from, current, to, base,
         units_error_string(result));
}

void convert_from_base(UnitsHandle handle, const char *dimension, double from) {
  UnitsErrorCode result;
  double to = 0.0;
  result = units_convert_from_base(handle, from, dimension, &to);
  char base[256] = {'\0'};
  units_get_base_unit(handle, dimension, base, 256);
  char current[256] = {'\0'};
  units_get_current_unit(handle, dimension, current, 256);
  printf("%s base unit: %s, current unit: %s\n", dimension, base, current);
  printf("%.4f %s (base unit) = %.4f %s (%s)\n", from, base, to, current,
         units_error_string(result));
}

int main() {
  // 1. 创建单位系统实例
  UnitsHandle handle = 0;
  units_create(&handle);
  if (!handle) {
    fprintf(stderr, "Failed to create units handle\n");
    return EXIT_FAILURE;
  }

  // 设置日志回调
  units_set_logger(handle, my_logger);

  // 2. 加载预定义单位（假设有配置文件）
  units_load_config_file(handle, "units_saved.cfg");

  // 3. 基本单位转换示例
  printf("\n=== 基本单位转换 ===\n");
  UnitsErrorCode result;
  double value = 0.0;

  // 温度转换
  result = units_convert(handle, 100.0, "celsius", "fahrenheit", &value);
  printf("100°C = %.2f°F (%s)\n", value, units_error_string(result));

  // 长度转换
  result = units_convert(handle, 1.0, "meter", "foot", &value);
  printf("1 meter = %.4f feet (%s)\n", value, units_error_string(result));

  // 4. 基础单位转换示例
  printf("\n=== 基础单位转换 ===\n");
  convert_to_base(handle, "length", 1000.0);

  // 5. 自定义单位注册
  printf("\n=== 自定义单位 ===\n");
  units_register_defined_unit(handle, "myunit", "meter * 0.5");
  result = units_convert(handle, 10.0, "myunit", "foot", &value);
  printf("10 myunit = %.4f feet (%s)\n", value, units_error_string(result));

  units_register_defined_unit(handle, "myunit2", "0.5foot");
  result = units_convert(handle, 10.0, "myunit2", "foot", &value);
  printf("10 myunit2 = %.4f feet (%s)\n", value, units_error_string(result));

  // 6. 单位管理
  printf("\n=== 单位管理 ===\n");

  units_set_base_unit(handle, "length", "meter");
  units_set_current_unit(handle, "length", "centimeter");
  convert_to_base(handle, "length", 100.0);
  convert_from_base(handle, "length", 100.0);

  units_set_base_unit(handle, "pressure", "MPa");
  units_set_current_unit(handle, "pressure", "bar");
  convert_to_base(handle, "pressure", 100.0);
  convert_from_base(handle, "pressure", 100.0);

  // 7. 序列化测试
  printf("\n=== 配置保存 ===\n");
  units_save_config_file(handle, "units_saved.cfg");
  printf("Configuration saved to units_saved.cfg\n");

  // 8. 清理资源
  units_destroy(handle);
  printf("\nUnits system destroyed\n");

  test_unit_system();

  return EXIT_SUCCESS;
}